/*
 * Device Tree Source for the Lager board
 *
 * Copyright (C) 2013-2014 Renesas Solutions Corp.
 * Copyright (C) 2014 Cogent Embedded, Inc.
 * Copyright (C) 2015-2016 Renesas Electronics Corporation
 *
 * This file is licensed under the terms of the GNU General Public License
 * version 2.  This program is licensed "as is" without any warranty of any
 * kind, whether express or implied.
 */

/*
 * SSI-AK4643
 *
 * SW1: 1: AK4643
 *      2: CN22
 *      3: ADV7511
 *
 * This command is required when Playback/Capture
 *
 *	amixer set "LINEOUT Mixer DACL" on
 *	amixer set "DVC Out" 100%
 *	amixer set "DVC In" 100%
 *
 * You can use Mute
 *
 *	amixer set "DVC Out Mute" on
 *	amixer set "DVC In Mute" on
 *
 * You can use Volume Ramp
 *
 *	amixer set "DVC Out Ramp Up Rate"   "0.125 dB/64 steps"
 *	amixer set "DVC Out Ramp Down Rate" "0.125 dB/512 steps"
 *	amixer set "DVC Out Ramp" on
 *	aplay xxx.wav &
 *	amixer set "DVC Out"  80%  // Volume Down
 *	amixer set "DVC Out" 100%  // Volume Up
 */

/dts-v1/;
#include "r8a7790.dtsi"
#include <dt-bindings/gpio/gpio.h>
#include <dt-bindings/input/input.h>

/ {
	model = "Lager";
	compatible = "renesas,lager", "renesas,r8a7790";

	aliases {
		serial0 = &scif0;
		serial1 = &scifa1;
		i2c8 = &gpioi2c1;
		i2c10 = &i2cexio0;
		i2c11 = &i2cexio1;
	};

	chosen {
		bootargs = "ignore_loglevel rw root=/dev/nfs ip=dhcp";
		stdout-path = "serial0:115200n8";
	};

	memory@40000000 {
		device_type = "memory";
		reg = <0 0x40000000 0 0x40000000>;
	};

	memory@140000000 {
		device_type = "memory";
		reg = <1 0x40000000 0 0xc0000000>;
	};

	lbsc {
		#address-cells = <1>;
		#size-cells = <1>;
	};

	keyboard {
		compatible = "gpio-keys";

		one {
			linux,code = <KEY_1>;
			label = "SW2-1";
			wakeup-source;
			debounce-interval = <20>;
			gpios = <&gpio1 14 GPIO_ACTIVE_LOW>;
		};
		two {
			linux,code = <KEY_2>;
			label = "SW2-2";
			wakeup-source;
			debounce-interval = <20>;
			gpios = <&gpio1 24 GPIO_ACTIVE_LOW>;
		};
		three {
			linux,code = <KEY_3>;
			label = "SW2-3";
			wakeup-source;
			debounce-interval = <20>;
			gpios = <&gpio1 26 GPIO_ACTIVE_LOW>;
		};
		four {
			linux,code = <KEY_4>;
			label = "SW2-4";
			wakeup-source;
			debounce-interval = <20>;
			gpios = <&gpio1 28 GPIO_ACTIVE_LOW>;
		};
	};

	leds {
		compatible = "gpio-leds";
		led6 {
			gpios = <&gpio4 22 GPIO_ACTIVE_HIGH>;
		};
		led7 {
			gpios = <&gpio4 23 GPIO_ACTIVE_HIGH>;
		};
		led8 {
			gpios = <&gpio5 17 GPIO_ACTIVE_HIGH>;
		};
	};

	fixedregulator3v3: regulator-3v3 {
		compatible = "regulator-fixed";
		regulator-name = "fixed-3.3V";
		regulator-min-microvolt = <3300000>;
		regulator-max-microvolt = <3300000>;
		regulator-boot-on;
		regulator-always-on;
	};

	vcc_sdhi0: regulator-vcc-sdhi0 {
		compatible = "regulator-fixed";

		regulator-name = "SDHI0 Vcc";
		regulator-min-microvolt = <3300000>;
		regulator-max-microvolt = <3300000>;

		gpio = <&gpio5 24 GPIO_ACTIVE_HIGH>;
		enable-active-high;
	};

	vccq_sdhi0: regulator-vccq-sdhi0 {
		compatible = "regulator-gpio";

		regulator-name = "SDHI0 VccQ";
		regulator-min-microvolt = <1800000>;
		regulator-max-microvolt = <3300000>;

		gpios = <&gpio5 29 GPIO_ACTIVE_HIGH>;
		gpios-states = <1>;
		states = <3300000 1
			  1800000 0>;
	};

	vcc_sdhi2: regulator-vcc-sdhi2 {
		compatible = "regulator-fixed";

		regulator-name = "SDHI2 Vcc";
		regulator-min-microvolt = <3300000>;
		regulator-max-microvolt = <3300000>;

		gpio = <&gpio5 25 GPIO_ACTIVE_HIGH>;
		enable-active-high;
	};

	vccq_sdhi2: regulator-vccq-sdhi2 {
		compatible = "regulator-gpio";

		regulator-name = "SDHI2 VccQ";
		regulator-min-microvolt = <1800000>;
		regulator-max-microvolt = <3300000>;

		gpios = <&gpio5 30 GPIO_ACTIVE_HIGH>;
		gpios-states = <1>;
		states = <3300000 1
			  1800000 0>;
	};

	audio_clock: audio_clock {
		compatible = "fixed-clock";
		#clock-cells = <0>;
		clock-frequency = <11289600>;
	};

	rsnd_ak4643: sound {
		compatible = "simple-audio-card";

		simple-audio-card,format = "left_j";
		simple-audio-card,bitclock-master = <&sndcodec>;
		simple-audio-card,frame-master = <&sndcodec>;

		sndcpu: simple-audio-card,cpu {
			sound-dai = <&rcar_sound>;
		};

		sndcodec: simple-audio-card,codec {
			sound-dai = <&ak4643>;
			clocks = <&audio_clock>;
		};
	};

	vga-encoder {
		compatible = "adi,adv7123";

		ports {
			#address-cells = <1>;
			#size-cells = <0>;

			port@0 {
				reg = <0>;
				adv7123_in: endpoint {
					remote-endpoint = <&du_out_rgb>;
				};
			};
			port@1 {
				reg = <1>;
				adv7123_out: endpoint {
					remote-endpoint = <&vga_in>;
				};
			};
		};
	};

	vga {
		compatible = "vga-connector";

		port {
			vga_in: endpoint {
				remote-endpoint = <&adv7123_out>;
			};
		};
	};

	hdmi-in {
		compatible = "hdmi-connector";
		type = "a";

		port {
			hdmi_con_in: endpoint {
				remote-endpoint = <&adv7612_in>;
			};
		};
	};

	hdmi-out {
		compatible = "hdmi-connector";
		type = "a";

		port {
			hdmi_con_out: endpoint {
				remote-endpoint = <&adv7511_out>;
			};
		};
	};

	x2_clk: x2-clock {
		compatible = "fixed-clock";
		#clock-cells = <0>;
		clock-frequency = <148500000>;
	};

	x13_clk: x13-clock {
		compatible = "fixed-clock";
		#clock-cells = <0>;
		clock-frequency = <148500000>;
	};

	gpioi2c1: i2c-8 {
		#address-cells = <1>;
		#size-cells = <0>;
		compatible = "i2c-gpio";
		status = "disabled";
		gpios = <&gpio1 17 GPIO_ACTIVE_HIGH /* sda */
			 &gpio1 16 GPIO_ACTIVE_HIGH /* scl */
			>;
		i2c-gpio,delay-us = <5>;
	};

	/*
	 * IIC0/I2C0 is routed to EXIO connector A, pins 114 (SCL) + 116 (SDA) only.
	 * We use the I2C demuxer, so the desired IP core can be selected at runtime
	 * depending on the use case (e.g. DMA with IIC0 or slave support with I2C0).
	 * Note: For testing the I2C slave feature, it is convenient to connect this
	 * bus with IIC3 on pins 110 (SCL) + 112 (SDA), select I2C0 at runtime, and
	 * instantiate the slave device at runtime according to the documentation.
	 * You can then communicate with the slave via IIC3.
	 *
	 * IIC0/I2C0 does not appear to support fallback to GPIO.
	 */
	i2cexio0: i2c-10 {
		compatible = "i2c-demux-pinctrl";
		i2c-parent = <&iic0>, <&i2c0>;
		i2c-bus-name = "i2c-exio0";
		#address-cells = <1>;
		#size-cells = <0>;
	};

	/*
	 * IIC1/I2C1 is routed to EXIO connector A, pins 78 (SCL) + 80 (SDA).
	 * This is similar to the arangement described for i2cexio0 (above)
	 * with a fallback to GPIO also provided.
	 */
	i2cexio1: i2c-11 {
		compatible = "i2c-demux-pinctrl";
		i2c-parent = <&iic1>, <&i2c1>, <&gpioi2c1>;
		i2c-bus-name = "i2c-exio1";
		#address-cells = <1>;
		#size-cells = <0>;
	};
};

&du {
	pinctrl-0 = <&du_pins>;
	pinctrl-names = "default";
	status = "okay";

	clocks = <&mstp7_clks R8A7790_CLK_DU0>,
		 <&mstp7_clks R8A7790_CLK_DU1>,
		 <&mstp7_clks R8A7790_CLK_DU2>,
		 <&mstp7_clks R8A7790_CLK_LVDS0>,
		 <&mstp7_clks R8A7790_CLK_LVDS1>,
		 <&x13_clk>, <&x2_clk>;
	clock-names = "du.0", "du.1", "du.2", "lvds.0", "lvds.1",
		      "dclkin.0", "dclkin.1";

	ports {
		port@0 {
			endpoint {
				remote-endpoint = <&adv7123_in>;
			};
		};
		port@1 {
			endpoint {
				remote-endpoint = <&adv7511_in>;
			};
		};
		port@2 {
			lvds_connector: endpoint {
			};
		};
	};
};

&extal_clk {
	clock-frequency = <20000000>;
};

&pfc {
	pinctrl-0 = <&scif_clk_pins>;
	pinctrl-names = "default";

	du_pins: du {
		groups = "du_rgb666", "du_sync_1", "du_clk_out_0";
		function = "du";
	};

	scif0_pins: scif0 {
		groups = "scif0_data";
		function = "scif0";
	};

	scif_clk_pins: scif_clk {
		groups = "scif_clk";
		function = "scif_clk";
	};

	ether_pins: ether {
		groups = "eth_link", "eth_mdio", "eth_rmii";
		function = "eth";
	};

	phy1_pins: phy1 {
		groups = "intc_irq0";
		function = "intc";
	};

	scifa1_pins: scifa1 {
		groups = "scifa1_data";
		function = "scifa1";
	};

	sdhi0_pins: sd0 {
		groups = "sdhi0_data4", "sdhi0_ctrl";
		function = "sdhi0";
		power-source = <3300>;
	};

	sdhi0_pins_uhs: sd0_uhs {
		groups = "sdhi0_data4", "sdhi0_ctrl";
		function = "sdhi0";
		power-source = <1800>;
	};

	sdhi2_pins: sd2 {
		groups = "sdhi2_data4", "sdhi2_ctrl";
		function = "sdhi2";
		power-source = <3300>;
	};

	sdhi2_pins_uhs: sd2_uhs {
		groups = "sdhi2_data4", "sdhi2_ctrl";
		function = "sdhi2";
		power-source = <1800>;
	};

	mmc1_pins: mmc1 {
		groups = "mmc1_data8", "mmc1_ctrl";
		function = "mmc1";
	};

	qspi_pins: qspi {
		groups = "qspi_ctrl", "qspi_data4";
		function = "qspi";
	};

	msiof1_pins: msiof1 {
		groups = "msiof1_clk", "msiof1_sync", "msiof1_rx",
				 "msiof1_tx";
		function = "msiof1";
	};

	i2c0_pins: i2c0 {
		groups = "i2c0";
		function = "i2c0";
	};

	iic0_pins: iic0 {
		groups = "iic0";
		function = "iic0";
	};

	i2c1_pins: i2c1 {
		groups = "i2c1";
		function = "i2c1";
	};

	iic1_pins: iic1 {
		groups = "iic1";
		function = "iic1";
	};

	iic2_pins: iic2 {
		groups = "iic2";
		function = "iic2";
	};

	iic3_pins: iic3 {
		groups = "iic3";
		function = "iic3";
	};

	hsusb_pins: hsusb {
		groups = "usb0_ovc_vbus";
		function = "usb0";
	};

	usb0_pins: usb0 {
		groups = "usb0";
		function = "usb0";
	};

	usb1_pins: usb1 {
		groups = "usb1";
		function = "usb1";
	};

	usb2_pins: usb2 {
		groups = "usb2";
		function = "usb2";
	};

	vin0_pins: vin0 {
		groups = "vin0_data24", "vin0_sync", "vin0_clkenb", "vin0_clk";
		function = "vin0";
	};

	vin1_pins: vin1 {
		groups = "vin1_data8", "vin1_clk";
		function = "vin1";
	};

	sound_pins: sound {
		groups = "ssi0129_ctrl", "ssi0_data", "ssi1_data";
		function = "ssi";
	};

	sound_clk_pins: sound_clk {
		groups = "audio_clk_a";
		function = "audio_clk";
	};
};

&ether {
	pinctrl-0 = <&ether_pins &phy1_pins>;
	pinctrl-names = "default";

	phy-handle = <&phy1>;
	renesas,ether-link-active-low;
	status = "okay";

	phy1: ethernet-phy@1 {
		reg = <1>;
		interrupt-parent = <&irqc0>;
		interrupts = <0 IRQ_TYPE_LEVEL_LOW>;
		micrel,led-mode = <1>;
	};
};

&cmt0 {
	status = "okay";
};

&mmcif1 {
	pinctrl-0 = <&mmc1_pins>;
	pinctrl-names = "default";

	vmmc-supply = <&fixedregulator3v3>;
	bus-width = <8>;
	non-removable;
	status = "okay";
};

&sata1 {
	status = "okay";
};

&qspi {
	pinctrl-0 = <&qspi_pins>;
	pinctrl-names = "default";

	status = "okay";

	flash: flash@0 {
		compatible = "spansion,s25fl512s", "jedec,spi-nor";
		reg = <0>;
		spi-max-frequency = <30000000>;
		spi-tx-bus-width = <4>;
		spi-rx-bus-width = <4>;
		spi-cpha;
		spi-cpol;
		m25p,fast-read;

		partitions {
			compatible = "fixed-partitions";
			#address-cells = <1>;
			#size-cells = <1>;

			partition@0 {
				label = "loader";
				reg = <0x00000000 0x00040000>;
				read-only;
			};
			partition@40000 {
				label = "user";
				reg = <0x00040000 0x00400000>;
				read-only;
			};
			partition@440000 {
				label = "flash";
				reg = <0x00440000 0x03bc0000>;
			};
		};
	};
};

&scif0 {
	pinctrl-0 = <&scif0_pins>;
	pinctrl-names = "default";

	status = "okay";
};

&scifa1 {
	pinctrl-0 = <&scifa1_pins>;
	pinctrl-names = "default";

	status = "okay";
};

&scif_clk {
	clock-frequency = <14745600>;
	status = "okay";
};

&msiof1 {
	pinctrl-0 = <&msiof1_pins>;
	pinctrl-names = "default";

	status = "okay";

	pmic: pmic@0 {
		compatible = "renesas,r2a11302ft";
		reg = <0>;
		spi-max-frequency = <6000000>;
		spi-cpol;
		spi-cpha;
	};
};

&sdhi0 {
	pinctrl-0 = <&sdhi0_pins>;
	pinctrl-1 = <&sdhi0_pins_uhs>;
	pinctrl-names = "default", "state_uhs";

	vmmc-supply = <&vcc_sdhi0>;
	vqmmc-supply = <&vccq_sdhi0>;
	cd-gpios = <&gpio3 6 GPIO_ACTIVE_LOW>;
	sd-uhs-sdr50;
	sd-uhs-sdr104;
	status = "okay";
};

&sdhi2 {
	pinctrl-0 = <&sdhi2_pins>;
	pinctrl-1 = <&sdhi2_pins_uhs>;
	pinctrl-names = "default", "state_uhs";

	vmmc-supply = <&vcc_sdhi2>;
	vqmmc-supply = <&vccq_sdhi2>;
	cd-gpios = <&gpio3 22 GPIO_ACTIVE_LOW>;
	sd-uhs-sdr50;
	status = "okay";
};

&cpu0 {
	cpu0-supply = <&vdd_dvfs>;
};

&i2c0	{
	pinctrl-0 = <&i2c0_pins>;
	pinctrl-names = "i2c-exio0";
};

&iic0	{
	pinctrl-0 = <&iic0_pins>;
	pinctrl-names = "i2c-exio0";
};

&i2c1	{
	pinctrl-0 = <&i2c1_pins>;
	pinctrl-names = "i2c-exio1";
};

&iic1	{
	pinctrl-0 = <&iic1_pins>;
	pinctrl-names = "i2c-exio1";
};

&iic2	{
	status = "okay";
	pinctrl-0 = <&iic2_pins>;
	pinctrl-names = "default";

	clock-frequency = <100000>;

	ak4643: codec@12 {
		compatible = "asahi-kasei,ak4643";
		#sound-dai-cells = <0>;
		reg = <0x12>;
	};

	composite-in@20 {
		compatible = "adi,adv7180";
		reg = <0x20>;
		remote = <&vin1>;

		port {
			adv7180: endpoint {
				bus-width = <8>;
				remote-endpoint = <&vin1ep0>;
			};
		};
	};

	hdmi@39 {
		compatible = "adi,adv7511w";
		reg = <0x39>;
		interrupt-parent = <&gpio1>;
		interrupts = <15 IRQ_TYPE_LEVEL_LOW>;

		adi,input-depth = <8>;
		adi,input-colorspace = "rgb";
		adi,input-clock = "1x";
		adi,input-style = <1>;
		adi,input-justification = "evenly";

		ports {
			#address-cells = <1>;
			#size-cells = <0>;

			port@0 {
				reg = <0>;
				adv7511_in: endpoint {
					remote-endpoint = <&du_out_lvds0>;
				};
			};

			port@1 {
				reg = <1>;
				adv7511_out: endpoint {
					remote-endpoint = <&hdmi_con_out>;
				};
			};
		};
	};

	hdmi-in@4c {
		compatible = "adi,adv7612";
		reg = <0x4c>;
		interrupt-parent = <&gpio1>;
		interrupts = <20 IRQ_TYPE_LEVEL_LOW>;
		default-input = <0>;

		ports {
			#address-cells = <1>;
			#size-cells = <0>;

			port@0 {
				reg = <0>;
				adv7612_in: endpoint {
					remote-endpoint = <&hdmi_con_in>;
				};
			};

			port@2 {
				reg = <2>;
				adv7612_out: endpoint {
					remote-endpoint = <&vin0ep2>;
				};
			};
		};
	};
};

&iic3 {
	pinctrl-names = "default";
	pinctrl-0 = <&iic3_pins>;
	status = "okay";

	pmic@58 {
		compatible = "dlg,da9063";
		reg = <0x58>;
		interrupt-parent = <&irqc0>;
		interrupts = <2 IRQ_TYPE_LEVEL_LOW>;
		interrupt-controller;

		rtc {
			compatible = "dlg,da9063-rtc";
		};

		wdt {
			compatible = "dlg,da9063-watchdog";
		};
	};

	vdd_dvfs: regulator@68 {
		compatible = "dlg,da9210";
		reg = <0x68>;
		interrupt-parent = <&irqc0>;
		interrupts = <2 IRQ_TYPE_LEVEL_LOW>;

		regulator-min-microvolt = <1000000>;
		regulator-max-microvolt = <1000000>;
		regulator-boot-on;
		regulator-always-on;
	};
};

&pci0 {
	status = "okay";
	pinctrl-0 = <&usb0_pins>;
	pinctrl-names = "default";
};

&pci1 {
	status = "okay";
	pinctrl-0 = <&usb1_pins>;
	pinctrl-names = "default";
};

&xhci {
	status = "okay";
	pinctrl-0 = <&usb2_pins>;
	pinctrl-names = "default";
};

&pci2 {
	status = "okay";
	pinctrl-0 = <&usb2_pins>;
	pinctrl-names = "default";
};

&hsusb {
	status = "okay";
	pinctrl-0 = <&hsusb_pins>;
	pinctrl-names = "default";
	renesas,enable-gpio = <&gpio5 18 GPIO_ACTIVE_HIGH>;
};

&usbphy {
	status = "okay";
};

/* HDMI video input */
&vin0 {
	pinctrl-0 = <&vin0_pins>;
	pinctrl-names = "default";

	status = "okay";

	port {
		vin0ep2: endpoint {
			remote-endpoint = <&adv7612_out>;
			bus-width = <24>;
			hsync-active = <0>;
			vsync-active = <0>;
			pclk-sample = <1>;
			data-active = <1>;
		};
	};
};

/* composite video input */
&vin1 {
	pinctrl-0 = <&vin1_pins>;
	pinctrl-names = "default";

	status = "okay";

	port {
		#address-cells = <1>;
		#size-cells = <0>;

		vin1ep0: endpoint {
			remote-endpoint = <&adv7180>;
			bus-width = <8>;
		};
	};
};

&rcar_sound {
	pinctrl-0 = <&sound_pins &sound_clk_pins>;
	pinctrl-names = "default";

	/* Single DAI */
	#sound-dai-cells = <0>;

	status = "okay";

	rcar_sound,dai {
		dai0 {
			playback = <&ssi0 &src2 &dvc0>;
			capture  = <&ssi1 &src3 &dvc1>;
		};
	};
};

&ssi1 {
	shared-pin;
};
